/**
 * Created by Admin on 2018/1/4.
 */
define(function (require, exports, module) {
    var template = require("template");
    var loadingTpl = require("../common/tpl/loading.html");
    require("../common/highchart/highcharts");
    require("../common/highchart/linechart");

    var statistic = {
        initTable: function (url,exportUrl,uriData, tpl, htmlContainer,title,linetitleD) {
            $(htmlContainer).html(template.compile(loadingTpl));
            $.ajax({
                url: url + "/" + uriData,
                success: function (data) {
                    $(".la-square-jelly-box.la-2x").hide();
                    if (data.code == 0 && data.infos != null) {
                        $("#Js_statisticalBox").show();
                        $(".nodata-title").hide();
                        var html = template.compile(tpl)({data: data})
                        $(htmlContainer).html(html);
                        var sdata = sortObject(data.infos[data.infos.length - 1]);
                        var sourceData = [{
                            name:title,
                            data:sdata,
                        }]
                        var year;
                        if(uriData == 0){
                            year = new Date().getFullYear()-1;
                        }else{
                            year = new Date().getFullYear();
                        }
                        var maxy = 0;
                        if (sdata != null && sdata.length > 0) {
                            for (var i = 0; i < sdata.length; i++) {
                                maxy = sdata[i];
                                if (maxy < sdata[i + 1]) {
                                    maxy = sdata[i + 1];
                                }
                                var linechart = new LineChart({
                                    dom: "ptamountchart",
                                    xData: ["1月累计", "2月累计", "3月累计", "4月累计", "5月累计", "6月累计", "7月累计", "8月累计", "9月累计", "10月累计", "11月累计", "12月累计"],
                                    yData: sourceData,
                                    linetitle: year+"年"+title,
                                    linetitleD: linetitleD,
                                    linetitleDClass: "p-t-question-chart",
                                    maxy: maxy,
                                    plotLines: maxy,
                                    enabled: false
                                })
                            }
                        }
                    }else{
                        $("#Js_statisticalBox").hide();
                        $(".nodata-title").show();
                        $("#ptamountchart").html("<div class='nodata-title' style='height:270px;line-height:270px;'>对不起，暂无数据!</div>");
                    }
                }
            });
            $("body").on("click",".btnExport",function(){
                window.location.href = exportUrl+"/"+uriData+"?"+api_key+"="+token;
            })
        },

    }
    function sortObject(obj) {
        var arr = ["jan","feb","mar","apr","may","june","july","aug","setp","oct","nov","dec"];
        var newArr = [];
        for (var key in obj) {
            if(arr.indexOf(key)>-1){
                newArr[arr.indexOf(key)] = obj[key];
            }
        }
        return newArr;
    }

    module.exports = statistic;
})